/*
    This file is part of the substandard lisp implementation of Standard Lisp
    Copyright (C) 2011 Nicky Nickell

    substandard lisp is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
    builtins related to the interpreter as defined in section 3.14 
    of the The Standard Lisp Report
*/

function std_apply(a, env) {
  var fn = a[0];
  var args = a[1];

  return evaluate(APPLYN(fn, args), env);
}
builtins['apply'] = new builtin_function(std_apply, 2, false, [TYPE_ID | TYPE_FUNCTION, TYPE_PAIR], FTYPE_EXPR);

function std_eval(a, env) {
  var u = a[0];

  return evaluate(u, env);
}
builtins['eval'] = new builtin_function(std_eval, 1, false, [CLASS_ANY], FTYPE_EXPR);

function std_elvis(a, env) {
  var u = a[0];

  var head = new lisp_pair(evaluate(u.car, env), NIL);
  var ptr = head;

  for (var p = u.cdr; p != NIL; p = p.cdr) {
    ptr.cdr = new lisp_pair(evaluate(p.car, env), NIL);
    ptr = ptr.cdr;
  }

  return head;
}
builtins['elvis'] = new builtin_function(std_elvis, 1, false, [CLASS_ANY], FTYPE_EXPR);

interpreted_builtins['expand'] = 
"(de expand (l fn) \
  (cond \
    ((null (cdr l)) (car l)) \
    (T (list fn (car l) (expand (cdr l) fn)))))";

function std_function(a, env) {
  var u = a[0];

  if (u.type == TYPE_ID) {
    return env.functions.get(u.value);
  } else {
    return u;
  }
}
builtins['function'] = new builtin_function(std_function, 1, false, [TYPE_FUNCTION | TYPE_ID], FTYPE_FEXPR);

function std_quote(a, env) {
  var u = a[0];

  return u;
}
builtins['quote'] = new builtin_function(std_quote, 1, false, [CLASS_ANY], FTYPE_FEXPR);
